# /usr/bin/python3
#-*-coding:utf-8-*-
# 运行环境： ubuntu 16.04 LTS/win 10
# tensorflow 1.13.1
# opencv 3.4.1.15
# 数据集： DeepFashion/Category and Attribute Prediction Benchmark
# 查看DeepFashion数据集中的类的样本数, 以及是否做了标定框
import cv2
import matplotlib as mt
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import os
import random
import colorsys
import time
import PIL
import skimage.io as io
import xml.etree.ElementTree as ET

# 这里顺序要放在最后, 不然这个界面显示的GUI不会生效
mt.use('TkAgg');

# 测试原先数据集的标注信息是否满足我的需求
test_bbox_of_the_picture = True;
test_dir = 'test_the_bbox_of_the_picture_in_fashion_data';
bbox_text = 'Anno/list_bbox.txt';
bbox_path = test_dir + '/' + bbox_text;
label_name = None;

if test_bbox_of_the_picture == True:
    with open(bbox_path, 'r', encoding='utf-8') as r:
        txt = r.readlines();
        print('存储数据的变量类型： ', type(txt));
        print('存储的变量长度： ', len(txt));
        annotations = [data.split() for data in txt if len(data.split()[1:]) != 0];
        print(len(annotations));
        print(annotations[1]);
        clothing_name = annotations[144254][0].split('/');
        # 读出文件的名字, 也就是该款服饰的名称
        print(clothing_name[1]);
        label_name = clothing_name[1];

def draw_bbox(image, bboxes, classes=[label_name], show_label=True):
    """
    bboxes: [x_min, y_min, x_max, y_max, probability, cls_id] format coordinates.
    """

    num_classes = len(classes)
    image_h, image_w, _ = image.shape
    hsv_tuples = [(1.0 * x / num_classes, 1., 1.) for x in range(num_classes)]
    colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
    colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))

    random.seed(0)
    random.shuffle(colors)
    random.seed(None)

    for i, bbox in enumerate(bboxes):
        coor = np.array(bbox[:4], dtype=np.int32)
        fontScale = 0.5;
        score = bbox[4]
        class_ind = int(bbox[5])
        bbox_color = colors[class_ind]
        bbox_thick = int(0.6 * (image_h + image_w) / 600)
        c1, c2 = (coor[0], coor[1]), (coor[2], coor[3])
        cv2.rectangle(image, c1, c2, bbox_color, bbox_thick)

        if show_label:
            bbox_mess = '%s: %.2f' % (classes[class_ind], score);
            t_size = cv2.getTextSize(bbox_mess, 0, fontScale, thickness=bbox_thick//2)[0];
            cv2.rectangle(image, c1, (c1[0] + t_size[0], c1[1] - t_size[1] - 3), bbox_color, -1);  # filled

            cv2.putText(image, bbox_mess, (c1[0], c1[1]-2), cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale, (0, 0, 0), bbox_thick//2, lineType=cv2.LINE_AA);

    return image;

image_path = test_dir + '/' + annotations[144259][0];
#print(image_path);
#print(annotations[2][1:]);
xmin, ymin, xmax, ymax = annotations[144259][1:];
print(xmin, ymin, xmax, ymax);
I = io.imread(image_path);
probability = 1;
Class_ID = 0;
winder_bbox = [int(float(xmin)), int(float(ymin)), int(float(xmax)), int(float(ymax)), probability, int(Class_ID)];
winder_image = draw_bbox(I, [winder_bbox]);
plt.figure(1);
plt.imshow(winder_image);
plt.axis('off');
plt.show();

# 经过观察发现, 在Category and Attribute Prediction Benchmark 中的图片可以分为3个大类
# 分别是衣服、裤子、连衣裤 这三类;
# 通过查找文件 list_category_img.txt 文件中第二列的数字, 这个数字代表着它在 list_categoyr_cloth.txt 中标签的索引位置
# 然后找到该位置的索引, 查看该文件的第二列数字, 这里的数字只有1、2、3, 这三类, 至此才能断定该图片属于哪个大类;
# 高分辨率的图片是没有标定框信息的, 需要通过同时比对高分辨率图片相对于原图片的放大比例来计算高分辨率自己的标定框的坐标值